package o.json;

import java.util.ArrayList;
import java.util.Iterator;

public class JSONArray extends JSONValue implements Iterable<JSONValue> {
	private final ArrayList<JSONValue> values = new ArrayList<JSONValue>();

	public JSONValue getValue(int index) {
		return values.get(index);
	}
	public void addValue(JSONValue value) {
		values.add(value);
	}
	public void setValue(int index, JSONValue value) {
		values.set(index, value);
	}
	public void insertValue(int index, JSONValue value) {
		values.add(index, value);
	}
	public int getLength() {
		return values.size();
	}

	@Override
	public boolean isArray() {
		return true;
	}
	@Override
	public JSONArray getArray() throws JSONException {
		return this;
	}

	@Override
	public Iterator<JSONValue> iterator() {
		return values.iterator();
	}

	@Override
	public boolean equals(Object object) {
		if (object == this) return true;
		if (object instanceof JSONArray) {
			return equals((JSONArray) object);
		}

		return false;
	}
	private boolean equals(JSONArray that) {
		return this.values.equals(that.values);
	}
	@Override
	public int hashCode() {
		return values.hashCode();
	}

	@Override
	public String toString() {
		return "[Array "+ values +"]";
	}
}
